<template>
  <div class="project-manage-page">
    <main>
      <div class="header">
        <p class="title">项目管理</p>
        <div class="search-container">
          <div class="search-input">
            <el-input placeholder="项目名称/项目简称" v-model="condition"></el-input>
            <el-button class="search-btn" type="primary" @click="handleSearch">
              <i class="el-icon-search"></i>
            </el-button>
            <el-button type="primary" @click="handleRefresh">
              <i class="el-icon-refresh"></i>
            </el-button>
          </div>
          <div class="btns">
            <el-button v-if="hasTabAdd" type="primary" @click="handleAdd">新建项目</el-button>
          </div>
        </div>
      </div>
      <div class="table-container">
        <el-table
          v-loading="tableLoading"
          :data="tableData"
          row-key="id">
          <el-table-column label="序号" type="index" width="80"></el-table-column>
          <el-table-column prop="projectName" label="项目名称">
            <template slot-scope="scope">
              <span style="display: none;"></span>
              <span v-if="hasTableLink" class="btn" @click="$router.push('/project-detail?id=' + scope.row.projectId)">
                {{ scope.row.projectName }}
              </span>
              <span v-else>{{ scope.row.projectName }}</span>
            </template>
          </el-table-column>
          <el-table-column prop="projectAbbreviation" label="项目简称"></el-table-column>
          <el-table-column prop="projectType" label="工程类别"></el-table-column>
          <el-table-column prop="manageName" label="项目经理"></el-table-column>
          <el-table-column prop="managePhone" label="联系方式"></el-table-column>
          <el-table-column prop="action" label="操作" width="380">
            <template slot-scope="scope">
              <span style="display: none;"></span>
              <el-button
                v-if="hasTableSet"
                type="primary"
                size="small"
                @click="$router.push(`/permission?id=${scope.row.projectId}`)">
                模块授权
              </el-button>
              <el-button
                v-if="hasTableMember"
                type="primary"
                size="small"
                @click="handleSet(scope.row)">
                成员
              </el-button>
              <el-button
                v-if="hasTableEdit"
                type="primary"
                size="small"
                @click="handleEdit(scope.row)">
                编辑
              </el-button>
              <el-button
                v-if="hasTableDel"
                type="danger"
                size="small"
                @click="handleDelete(scope.row)">
                删除
              </el-button>
            </template>
          </el-table-column>
          <div class="empty-content" slot="empty">
            <i class="iconfont iconwushuju"></i>
            <p>暂无数据</p>
          </div>
        </el-table>
      </div>
    </main>
    <set-project
      :type="type"
      :options="currentData"
      :visible="visible"
      :loading="loading"
      @cancel="visible = false"
      @submit="handleSave">
    </set-project>
    <set-project-member
      :visible="memberVisible"
      :id="projectId"
      @cancel="handleCancel">
    </set-project-member>
  </div>
</template>

<script>
import setProject from '@/components/corp/setProject';
import setProjectMember from '@/components/corp/setProjectMember';

export default {
  components: {
    setProject,
    setProjectMember
  },
  data() {
    return {
      condition: '',
      hasSearch: false,
      pageNo: 1,
      pageSize: 10,
      total: 0,
      tableData: [],
      tableLoading: false,
      loading: false,
      visible: false,
      type: 'add',
      currentData: {},
      // 添加成员
      projectId: '',
      memberVisible: false
    };
  },
  watch: {
    visible(val) {
      if (!val) {
        this.loading = false;
      }
    },
  },
  created() {
    this.getTableData();
  },
  methods: {
    /**
     * 获取项目数据
     */
    async getTableData() {
      this.tableLoading = true;
      const { success, data: {
        data
      }} = await this.$Model.getProjectList({
        condition: this.hasSearch ? this.condition : ''
      });
      this.tableLoading = false;
      if (success) {
        this.tableData = data;
      }
    },
    clear() {
      this.pageNo = 1;
      this.pageSize = 10;
      this.total = 0;
    },
    handleSearch() {
      this.hasSearch = true;
      this.clear();
      this.getTableData();
    },
    handleRefresh() {
      this.hasSearch = false;
      this.clear();
      this.getTableData();
    },
    // 新建
    handleAdd() {
      this.currentData = {};
      this.type = 'add';
      this.visible = true;
    },
    // 编辑
    handleEdit(row) {
      this.currentData = { ...row };
      this.type = 'edit';
      this.visible = true;
    },
    // 设置成员
    handleSet(row) {
      this.projectId = row.projectId;
      this.memberVisible = true;
    },
    // 取消
    handleCancel() {
      this.memberVisible = false;
      this.projectId = '';
    },
    /**
     * 删除
     * @param {Object} row 当前数据
     */
    handleDelete(row) {
      this.$confirm('此操作将删除该项目, 是否继续?', '提示', {
        type: 'warning'
      }).then(async () => {
        this.tableLoading = true;
        const { success } = await this.$Model.delProject({
          projectId: row.projectId
        });
        this.tableLoading = false;
        if (success) {
          this.getTableData();
          this.$message.success('删除成功');
        }
      });
    },
    // 保存
    async handleSave(params) {
      this.loading = true;
      const { success } = await this.$Model.projectSet(params);
      this.loading = false;
      if (success) {
        this.getTableData();
        this.$message.success('保存成功');
        this.visible = false;
      }
    }
  }
};
</script>

<style scoped lang="scss">
  @import "../../assets/styles/table";
  @import "../../assets/styles/dialog";

  .project-manage-page {
    width: 100%;
    height: 100%;
    padding: 20px 30px;
    background-color: $baseBg;
    main {
      width: 100%;
      height: 100%;
      padding: 20px;
      background-color: $mainBg;
      .header {
        width: 100%;
        height: 100px;
        display: flex;
        flex-direction: column;
        justify-content: space-between;
        margin-bottom: 20px;
        .title {
          font-size: 20px;
        }
        .search-container {
          display: flex;
          justify-content: space-between;
          align-items: center;
          .search-input {
            display: flex;
            .tabs {
              display: flex;
              margin-right: 20px;
              box-shadow: 0 0 3px 0 $mainBg;
              li {
                width: 120px;
                background: #fff;
                color: rgba(0, 0, 0, .8);
                display: flex;
                justify-content: center;
                align-items: center;
                cursor: pointer;
                transition: all .2s;
                &.active, &:hover {
                  background: $linearG;
                  color: #fff;
                }
                &:nth-child(1) {
                  border-radius: 5px 0 0 5px;
                }
                &:nth-last-child(1) {
                  border-radius: 0 5px 5px 0;
                }
              }
            }
            .el-input {
              width: 260px;
              margin-right: 20px;
            }
          }
        }
        .btns {
          .upload-file {
            position: relative;
            cursor: pointer;
            .file {
              position: absolute;
              width: 100%;
              height: 100%;
              top: 0;
              left: 0;
              right: 0;
              bottom: 0;
              opacity: 0;
              cursor: pointer;
            }
          }
        }
      }
      .table-container {
        width: 100%;
        height: calc(100% - 100px);
        /deep/ .el-table {
          height: calc(100% - 50px);
          .el-table__body-wrapper {
            height: calc(100% - 40px);
            overflow-y: auto;
            .btn {
              color: $mainColor;
              cursor: pointer;
              &:hover {
                text-decoration: underline;
              }
            }
          }
        }
      }
    }
  }
</style>
